package GUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import Backend.Algorithmen.SuperAlgo;

public class ControlPanel extends JPanel{

	private JSpinner sizex = new JSpinner();
	private JSpinner sizey = new JSpinner();
	private JSpinner time = new JSpinner();

	private JLabel dsizex = new JLabel("Anzahl X Felder");
	private JLabel dsizey = new JLabel("Anzahl y Felder");
	private JLabel dtime = new JLabel("Delay (ms)");

	private JLabel dalgo_chooser = new JLabel("Algorithmus");
	private JLabel dummy = new JLabel();

	private JCheckBox slow = new JCheckBox("Step-by-Step",false);


	private JComboBox algo_chooser = new JComboBox();
	private JButton start = new JButton("Start");
	private JButton stop = new JButton("Stopp");
	private JButton plus = new JButton("+");
	private JButton minus = new JButton("-");

	private ArrayList<SizeChangedListener> sizelistener = new ArrayList<SizeChangedListener>();
	
	public ControlPanel(){
		init();
	}
	
	public void init(){
		GridLayout grid = new GridLayout(2,12);
		grid.setHgap(10);
		setLayout(grid);
		time.setEnabled(false);
		sizex.setValue(100);
		sizey.setValue(50);
		time.setValue(10);
		
		add(dsizex);
		add(dsizey);
		add(dalgo_chooser);
		add(slow);
		add(dtime);
		add(plus);
		add(start);

		add(sizex);
		add(sizey);
		add(algo_chooser);
		add(dummy);
		add(time);
		add(minus);
		add(stop);
		
		TitledBorder border = BorderFactory.createTitledBorder("Control Area");
		border.setTitlePosition(TitledBorder.CENTER);
		setBorder(border);
		
		stop.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				SuperAlgo s = (SuperAlgo)algo_chooser.getSelectedItem();
				if(s.isRunning()){
					s.setRunning(false);
				}
			}
		});
		
		slow.addActionListener(new ActionListener() {
			
			boolean enable = false;
			@Override
			public void actionPerformed(ActionEvent e) {
				enable = !enable;
				time.setEnabled(enable);
			}
		});
		
		minus.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				callSizeChangedListener(true);				
			}
		});
		
		plus.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				callSizeChangedListener(false);			
			}
		});
		
	}
	
	public void setComboData( List<SuperAlgo> algos){
		for(SuperAlgo s : algos){
			algo_chooser.addItem(s);
		}
	}
	
	public void setActionListener(MazeListener m){
		start.addActionListener(m);
	}
	
	public int getXCount(){
		return (Integer)sizex.getValue();
	}
	
	public int getYCount(){
		return (Integer)sizey.getValue();
		
	}
	
	public int getDelay(){
		return (Integer)time.getValue();
	}
	
	public SuperAlgo getAlgorithm(){
		return (SuperAlgo)algo_chooser.getSelectedItem();
	}
	
	public boolean getSpeed(){
		return slow.isSelected();
	}
	
	public void enableStartButton(boolean en){
		start.setEnabled(en);
	}
	
	public void addSizeChangedListener(SizeChangedListener s){
		sizelistener.add(s);
	}
	
	public void removeSizeChangedListener(SizeChangedListener s){
		sizelistener.remove(s);
	}
	
	public void callSizeChangedListener(boolean minus){
		for(SizeChangedListener scl : sizelistener){
			if(minus)
				scl.minus();
			if(!minus)
				scl.plus();
		}
	}
	
}
